// pages/homePage/homePage.ts
Page({

    /**
     * 页面的初始数据
     */
    data: {
        user: '',
        inputValue: '',
        // 有多少个需要处理的申请
        count: 0
    },
    onLoad() {
        this.getUserInfo();
    },
    async getUserInfo() {
        let user = wx.getStorageSync('user')
        if (user) {
            if (user.isAdmin) {
                this.getApproval()
            }
            const userCollection = wx.cloud.database().collection('user');
            const sqlUser = await userCollection.where({
                account: user.account
            }).limit(1).get();
            console.log('userInfo', sqlUser.data[0])
            this.setData({
                // @ts-ignore
                user: Object.assign(user, sqlUser.data[0])
            })
        }
    },
    async getApproval() {
        const db = wx.cloud.database();
        const res = await db.collection('request-permit-list').count();
        console.log(res.total);
        this.setData({
            count: res.total,
        });
    },
    // Logout
    //退出登陆后将storage中的user清空
    onLogout() {
        wx.showModal({
            title: 'Logout',
            content: 'Are you sure you want to log out?',
            confirmText: 'Confirm',
            cancelText: 'Cancel',
            success(res) {
                if (res.confirm) {
                    // 用户点击了确定按钮
                    // 在这里处理退出登录的逻辑
                    wx.clearStorageSync();//清除缓存
                    wx.showToast({
                        title: 'Log out successfully',
                        icon: 'none',
                        duration: 2000,
                        success: function () {
                            setTimeout(function () {
                                //跳转到首页，强制重启
                                wx.reLaunch({
                                    url: '/pages/login/login',
                                })
                            }, 1000);
                        }
                    })
                } else if (res.cancel) {
                    // 用户点击了取消按钮
                }
            }
        })
    },
    // 跳转到个人中心
    gotoCenter() {
        // @ts-ignore
        if (this.data.user.isAdmin) {
            wx.navigateTo({
                url: "/pages/personalCenter/personalCenter"
            })
        }
    },
    gotoSearch() {
        console.log(this.data.inputValue)
        if (!this.data.inputValue) return;
        const { user } = this.data;
        // @ts-ignore
        if (user && !user.canSearch) {
            wx.showModal({
                title: 'Authorization Tips',
                content: 'You do not have permission to search, do you want to apply?',
                confirmText: 'Confirm',
                cancelText: 'Cancel',
                success(res) {
                    if (res.confirm) {
                        wx.cloud.callFunction({
                            name: 'requestPermit',
                            data: {
                                // @ts-ignore
                                account: user.account || '',
                            },
                        }).then((res) => {
                            console.log(res);
                            //@ts-ignore
                            if (res.result.code === -1) {
                                wx.showToast({
                                    //@ts-ignore
                                    title: res.result.msg,
                                    icon: 'none'
                                })
                            } else {
                                wx.showToast({
                                    title: 'A request has been submitted to the administrator, please wait for approval!',
                                    icon: 'none'
                                })
                            }
                        }).catch(err => {
                            console.log(err)
                            wx.showToast({
                                title: 'Application failed, please try again later',
                                icon: 'none'
                            })
                        })
                    } else if (res.cancel) {
                        // 用户点击取消，提示用户
                        wx.showToast({
                            title: 'You have cancelled your application',
                            icon: 'none'
                        })
                    }
                }
            })
            return;
        }
        wx.navigateTo({
            url: '../logs/logs?search=' + this.data.inputValue,
        })
    },
    bindKeyInput: function (e: any) {
        this.setData({
            inputValue: e.detail.value
        })
    },
})